package com.imu.purse.modules.bitcoin;


import java.io.FileInputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.imu.purse.bean.CoreMoneyAddr;
import com.imu.purse.bean.YtfTransForm;
import com.xiaoleilu.hutool.crypto.SecureUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.alibaba.druid.util.Base64;
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.http.HttpService;

public class TestRpc {

	/**
	 * 初始化client
	 */
	private static  JsonRpcHttpClient initClient(){
		JsonRpcHttpClient client = null;
		try{
			Map<String,String> paramMap = Maps.newHashMap();
			//身份认证
			paramMap.put("Content-Type","application/json");
			client = new JsonRpcHttpClient(new URL("http://101.132.158.100:8545"));
			client.setHeaders(paramMap);
		}catch (Exception e){
			e.printStackTrace();
		}
		return client;
	}
	public static void main(String[] args){
		try{
			/*Map<String,String> map = new HashMap<String,String>();
			JsonRpcHttpClient client = new JsonRpcHttpClient(new URL("http://106.15.93.26:6699"));
			map.put("Content-Type","application/json");
			client.setHeaders(map);*/

			/*JsonRpcHttpClient client = initClient();

			YtfTransForm form = new YtfTransForm();
			form.setTo("0xf6a299c4faa33b0de390500a7a2e712c470eb970");
			form.setFrom("0x2cfdb20448e0fa47bc7e93ddca2238ff413f394b");
			form.setValue("0xDE0B6B3A7640000");
			client.invoke("personal_unlockAccount",new Object[]{"0x2cfdb20448e0fa47bc7e93ddca2238ff413f394b","18100000010"});
			Object result = client.invoke("eth_sendTransaction",new Object[]{JSON.toJSON(form)},Object.class);

			System.out.println("getbalance===================" + result);*/

			/*Web3j web3 = Web3j.build(new HttpService("https://etherscan.io"));
			Object obj = web3.ethGetTransactionReceipt("0xae0eedafbbb465613934c940ead32522da87761f9ca990b09470d24ba8469663").send();
			System.out.println("obj====" + obj);*/
			//System.out.println(new Date(1529903189l*1000l));

			//System.out.println(SecureUtil.md5("654321"));
			JsonRpcHttpClient client = initClient();
			Object result = client.invoke("personal_newAccount", new Object[]{"18100000050"},Object.class);
			System.out.println(result);
		}catch(Throwable e){
			System.out.println("================" + e.getMessage());
		}
        /**
         * 转账
         * 1、getinfo   
         * 2、validateaddress  参数为34位地址1GBwgTQXXrdq5efvCdSnnBQQADAGbMTYqf
         * 3、sendtoaddress   参数为34位地址{1GBwgTQXXrdq5efvCdSnnBQQADAGbMTYqf,"8" }
         * */
        
        /**
         * 获取数据
         * 1、getinfo 
         * 2、getblockcount  //区块高度
         * 3、getblockhash
         * 4、getblock
         * 5、gettransaction
         * 7、getaddressesbyaccount
         * 8、入本地库
         * 
         * */
    }

    private static void checkMoney(JsonRpcHttpClient client){
		XSSFWorkbook xssfWorkbook;
		try {
			xssfWorkbook = new XSSFWorkbook(new FileInputStream("d:\\02.xlsx"));
			for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
				XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
				if (xssfSheet == null) {
					continue;
				}
				// Read the Row
				for (int rowNum = 2; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
					XSSFRow xssfRow = xssfSheet.getRow(rowNum);
					if (xssfRow != null) {
						XSSFCell address = xssfRow.getCell(0);//昵称
						BigDecimal big = new BigDecimal(address.toString());
						Object obj = client.invoke("getbalance",new Object[]{big.toPlainString()},Object.class);
						if(obj != null){
							Double b = (Double)obj;
							if(b == 0){
								System.out.println("address:" + big + ",money : " + b);
							}
						}
					}
				}
			}

		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
    private static  void parseExcel(JsonRpcHttpClient client){
		XSSFWorkbook xssfWorkbook;
		try {
			xssfWorkbook = new XSSFWorkbook(new FileInputStream("d:\\01.xlsx"));
			for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
				XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
				if (xssfSheet == null) {
					continue;
				}
				// Read the Row
				for (int rowNum = 2; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
					XSSFRow xssfRow = xssfSheet.getRow(rowNum);
					if (xssfRow != null) {
						XSSFCell address = xssfRow.getCell(0);//昵称
						client.invoke("movetoaddress",new Object[]{"18186130905",address.toString(),100,0},Object.class);
					}
				}
			}

		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
